import {
  additionalParameterTipList,
  quantizationParametersTipList,
} from '../data/data'

export default function getModelFormConfig({
  t,
  formData,
  modelData,
  gpuAvailable,
  engineItems,
  formatItems,
  sizeItems,
  quantizationItems,
  getNGPURange,
  downloadHubOptions,
  enginesWithNWorker,
  multimodalProjectorOptions,
}) {
  const config = {
    LLM: [
      {
        name: 'model_engine',
        label: t('launchModel.modelEngine'),
        type: 'select',
        options: engineItems,
        visible: true,
        required: true,
      },
      {
        name: 'model_format',
        label: t('launchModel.modelFormat'),
        type: 'select',
        options: formatItems,
        visible: true,
        disabled: !formData.model_engine,
        required: true,
      },
      {
        name: 'model_size_in_billions',
        label: t('launchModel.modelSize'),
        type: 'select',
        options: sizeItems,
        visible: true,
        disabled: !formData.model_format,
        required: true,
      },
      {
        name: 'quantization',
        label: t('launchModel.quantization'),
        type: 'select',
        options: quantizationItems,
        visible: true,
        disabled: !formData.model_size_in_billions,
        required: true,
      },
      {
        name: 'multimodal_projector',
        label: t('launchModel.multimodelProjector'),
        type: 'select',
        default: multimodalProjectorOptions[0],
        options: multimodalProjectorOptions,
        disabled: !formData.model_size_in_billions,
        required: true,
        visible: !!multimodalProjectorOptions.length,
      },
      {
        name: 'n_gpu',
        label: t(
          enginesWithNWorker.includes(formData.model_engine)
            ? 'launchModel.nGPUPerWorker'
            : 'launchModel.nGPU'
        ),
        type: 'select',
        default: 'auto',
        options: getNGPURange('LLM'),
        disabled: !formData.quantization,
        visible: true,
      },
      {
        name: 'n_gpu_layers',
        label: t('launchModel.nGpuLayers'),
        type: 'number',
        default: -1,
        inputProps: {
          inputProps: {
            min: -1,
          },
        },
        disabled: !formData.quantization,
        required: true,
        visible: !!['ggufv2', 'ggmlv3'].includes(formData.model_format),
      },
      {
        name: 'replica',
        label: t('launchModel.replica'),
        type: 'number',
        default: 1,
        inputProps: {
          inputProps: {
            min: 1,
          },
        },
        disabled: !formData.quantization,
        visible: true,
      },
      {
        name: 'enable_thinking',
        label: t('launchModel.enableThinking'),
        type: 'switch',
        visible: !!modelData.model_ability?.includes('hybrid'),
        default: true,
      },
      {
        name: 'reasoning_content',
        label: t('launchModel.parsingReasoningContent'),
        type: 'switch',
        default: false,
      },
      {
        name: 'nested_section_optional',
        label: t('launchModel.optionalConfigurations'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'model_uid',
            label: t('launchModel.modelUID.optional'),
            type: 'input',
            visible: true,
          },
          {
            name: 'request_limits',
            label: t('launchModel.requestLimits.optional'),
            type: 'number',
            error:
              !!formData.request_limits &&
              !/^[1-9]\d*$/.test(formData.request_limits),
            helperText: t('launchModel.enterIntegerGreaterThanZero'),
            visible: true,
          },
          {
            name: 'n_worker',
            label: t('launchModel.workerCount.optional'),
            type: 'number',
            default: 1,
            inputProps: {
              inputProps: {
                min: 1,
              },
            },
            visible:
              !!formData.model_engine &&
              enginesWithNWorker.includes(formData.model_engine),
          },
          {
            name: 'worker_ip',
            label: t('launchModel.workerIp.optional'),
            type: 'input',
            visible: true,
          },
          {
            name: 'gpu_idx',
            label: t('launchModel.GPUIdx'),
            type: 'input',
            error:
              !!formData.gpu_idx && !/^\d+(?:,\d+)*$/.test(formData.gpu_idx),
            helperText: t('launchModel.enterCommaSeparatedNumbers'),
            visible: true,
          },
          {
            name: 'download_hub',
            label: t('launchModel.downloadHub.optional'),
            type: 'select',
            options: downloadHubOptions,
            visible: true,
          },
          {
            name: 'model_path',
            label: t('launchModel.modelPath.optional'),
            type: 'input',
            visible: true,
          },
          {
            name: 'nested_section_lora',
            label: t('launchModel.loraConfig'),
            type: 'collapse',
            visible: true,
            children: [
              {
                name: 'lora_list',
                label: t('launchModel.loraModelConfig'),
                type: 'dynamicField',
                mode: 'key-value',
                keyPlaceholder: 'lora_name',
                valuePlaceholder: 'local_path',
                visible: true,
              },
            ],
          },
          {
            name: 'nested_section_virtualEnv',
            label: t('launchModel.virtualEnvConfig'),
            type: 'collapse',
            visible: true,
            children: [
              {
                name: 'enable_virtual_env',
                label: t('launchModel.modelVirtualEnv'),
                type: 'radio',
                options: [
                  { label: 'Unset', value: 'unset' },
                  { label: 'False', value: false },
                  { label: 'True', value: true },
                ],
                default: 'unset',
                visible: true,
              },
              {
                name: 'virtual_env_packages',
                label: t('launchModel.virtualEnvPackage'),
                type: 'dynamicField',
                mode: 'value',
                valuePlaceholder: 'value',
                visible: true,
              },
            ],
          },
          {
            name: 'nested_section_env',
            label: t('launchModel.envVariableConfig'),
            type: 'collapse',
            visible: true,
            children: [
              {
                name: 'envs',
                label: t('launchModel.envVariable'),
                type: 'dynamicField',
                mode: 'key-value',
                keyPlaceholder: 'key',
                valuePlaceholder: 'value',
                visible: true,
              },
            ],
          },
        ],
      },
      {
        name: 'quantization_config',
        label: t(
          'launchModel.additionalQuantizationParametersForInferenceEngine'
        ),
        type: 'dynamicField',
        mode: 'key-value',
        keyPlaceholder: 'key',
        valuePlaceholder: 'value',
        keyOptions: quantizationParametersTipList,
        visible:
          !!formData.model_engine && formData.model_engine === 'Transformers',
      },
      {
        name: 'custom',
        label: `${t('launchModel.additionalParametersForInferenceEngine')}${
          formData.model_engine ? ': ' + formData.model_engine : ''
        }`,
        type: 'dynamicField',
        mode: 'key-value',
        keyPlaceholder: 'key',
        valuePlaceholder: 'value',
        keyOptions:
          additionalParameterTipList[
            formData.model_engine?.toLocaleLowerCase()
          ],
        visible: true,
      },
    ],
    embedding: [
      {
        name: 'model_engine',
        label: t('launchModel.modelEngine'),
        type: 'select',
        options: engineItems,
        visible: true,
      },
      {
        name: 'model_format',
        label: t('launchModel.modelFormat'),
        type: 'select',
        options: formatItems,
        visible: true,
        disabled: !formData.model_engine,
      },
      {
        name: 'quantization',
        label: t('launchModel.quantization'),
        type: 'select',
        options: quantizationItems,
        visible: true,
        disabled: !formData.model_format,
      },
      {
        name: 'replica',
        label: t('launchModel.replica'),
        type: 'number',
        visible: true,
        default: 1,
        inputProps: {
          inputProps: {
            min: 1,
          },
        },
        disabled: !formData.quantization,
      },
      {
        name: 'n_gpu',
        label: t('launchModel.device'),
        type: 'select',
        default: gpuAvailable === 0 ? 'CPU' : 'GPU',
        options: getNGPURange('embedding'),
        visible: true,
      },
      {
        name: 'gpu_idx',
        label: t('launchModel.GPUIdx'),
        type: 'input',
        error: !!formData.gpu_idx && !/^\d+(?:,\d+)*$/.test(formData.gpu_idx),
        helperText: t('launchModel.enterCommaSeparatedNumbers'),
        visible: !!formData.n_gpu && formData.n_gpu === 'GPU',
      },
      {
        name: 'model_uid',
        label: t('launchModel.modelUID.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'worker_ip',
        label: t('launchModel.workerIp.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'download_hub',
        label: t('launchModel.downloadHub.optional'),
        type: 'select',
        options: downloadHubOptions,
        visible: true,
      },
      {
        name: 'model_path',
        label: t('launchModel.modelPath.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'nested_section_virtualEnv',
        label: t('launchModel.virtualEnvConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'enable_virtual_env',
            label: t('launchModel.modelVirtualEnv'),
            type: 'radio',
            options: [
              { label: 'Unset', value: 'unset' },
              { label: 'False', value: false },
              { label: 'True', value: true },
            ],
            default: 'unset',
            visible: true,
          },
          {
            name: 'virtual_env_packages',
            label: t('launchModel.virtualEnvPackage'),
            type: 'dynamicField',
            mode: 'value',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'nested_section_env',
        label: t('launchModel.envVariableConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'envs',
            label: t('launchModel.envVariable'),
            type: 'dynamicField',
            mode: 'key-value',
            keyPlaceholder: 'key',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'custom',
        label: `${t('launchModel.additionalParametersForInferenceEngine')}${
          formData.model_engine ? ': ' + formData.model_engine : ''
        }`,
        type: 'dynamicField',
        mode: 'key-value',
        keyPlaceholder: 'key',
        valuePlaceholder: 'value',
        visible: true,
      },
    ],
    rerank: [
      {
        name: 'model_engine',
        label: t('launchModel.modelEngine'),
        type: 'select',
        options: engineItems,
        visible: true,
      },
      {
        name: 'model_format',
        label: t('launchModel.modelFormat'),
        type: 'select',
        options: formatItems,
        visible: true,
        disabled: !formData.model_engine,
      },
      {
        name: 'quantization',
        label: t('launchModel.quantization'),
        type: 'select',
        options: quantizationItems,
        visible: true,
        disabled: !formData.model_format,
      },
      {
        name: 'replica',
        label: t('launchModel.replica'),
        type: 'number',
        visible: true,
        default: 1,
        inputProps: {
          inputProps: {
            min: 1,
          },
        },
      },
      {
        name: 'n_gpu',
        label: t('launchModel.device'),
        type: 'select',
        default: gpuAvailable === 0 ? 'CPU' : 'GPU',
        options: getNGPURange('rerank'),
        visible: true,
      },
      {
        name: 'gpu_idx',
        label: t('launchModel.GPUIdx'),
        type: 'input',
        error: !!formData.gpu_idx && !/^\d+(?:,\d+)*$/.test(formData.gpu_idx),
        helperText: t('launchModel.enterCommaSeparatedNumbers'),
        visible: !!formData.n_gpu && formData.n_gpu === 'GPU',
      },
      {
        name: 'model_uid',
        label: t('launchModel.modelUID.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'worker_ip',
        label: t('launchModel.workerIp.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'download_hub',
        label: t('launchModel.downloadHub.optional'),
        type: 'select',
        options: downloadHubOptions,
        visible: true,
      },
      {
        name: 'model_path',
        label: t('launchModel.modelPath.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'nested_section_virtualEnv',
        label: t('launchModel.virtualEnvConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'enable_virtual_env',
            label: t('launchModel.modelVirtualEnv'),
            type: 'radio',
            options: [
              { label: 'Unset', value: 'unset' },
              { label: 'False', value: false },
              { label: 'True', value: true },
            ],
            default: 'unset',
            visible: true,
          },
          {
            name: 'virtual_env_packages',
            label: t('launchModel.virtualEnvPackage'),
            type: 'dynamicField',
            mode: 'value',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'nested_section_env',
        label: t('launchModel.envVariableConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'envs',
            label: t('launchModel.envVariable'),
            type: 'dynamicField',
            mode: 'key-value',
            keyPlaceholder: 'key',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'custom',
        label: t('launchModel.additionalParametersForInferenceEngine'),
        type: 'dynamicField',
        mode: 'key-value',
        keyPlaceholder: 'key',
        valuePlaceholder: 'value',
        visible: true,
      },
    ],
    image: [
      {
        name: 'model_uid',
        label: t('launchModel.modelUID.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'replica',
        label: t('launchModel.replica'),
        type: 'number',
        visible: true,
        default: 1,
        inputProps: {
          inputProps: {
            min: 1,
          },
        },
      },
      {
        name: 'n_gpu',
        label: t('launchModel.nGPU'),
        type: 'select',
        default: 'auto',
        options: getNGPURange('image'),
        visible: true,
      },
      {
        name: 'gpu_idx',
        label: t('launchModel.GPUIdx'),
        type: 'input',
        error: !!formData.gpu_idx && !/^\d+(?:,\d+)*$/.test(formData.gpu_idx),
        helperText: t('launchModel.enterCommaSeparatedNumbers'),
        visible: !!formData.n_gpu && formData.n_gpu !== 'CPU',
      },
      {
        name: 'worker_ip',
        label: t('launchModel.workerIp.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'download_hub',
        label: t('launchModel.downloadHub.optional'),
        type: 'select',
        options: downloadHubOptions,
        visible: true,
      },
      {
        name: 'model_path',
        label: t('launchModel.modelPath.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'gguf_quantization',
        label: t('launchModel.GGUFQuantization.optional'),
        type: 'select',
        options: ['none', ...(modelData.gguf_quantizations || [])],
        visible: !!modelData.gguf_quantizations,
      },
      {
        name: 'gguf_model_path',
        label: t('launchModel.GGUFModelPath.optional'),
        type: 'input',
        visible: !!modelData.gguf_quantizations,
      },
      {
        name: 'lightning_version',
        label: t('launchModel.lightningVersions.optional'),
        type: 'select',
        options: ['none', ...(modelData.lightning_versions || [])],
        visible: !!modelData.lightning_versions,
      },
      {
        name: 'lightning_model_path',
        label: t('launchModel.lightningModelPath.optional'),
        type: 'input',
        visible: !!modelData.lightning_versions,
      },
      {
        name: 'cpu_offload',
        label: t('launchModel.CPUOffload'),
        type: 'switch',
        default: false,
        tip: t('launchModel.CPUOffload.tip'),
        visible: true,
      },
      {
        name: 'nested_section_lora',
        label: t('launchModel.loraConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'lora_list',
            label: t('launchModel.loraModelConfig'),
            type: 'dynamicField',
            mode: 'key-value',
            keyPlaceholder: 'lora_name',
            valuePlaceholder: 'local_path',
            visible: true,
          },
          {
            name: 'image_lora_load_kwargs',
            label: t('launchModel.loraLoadKwargsForImageModel'),
            type: 'dynamicField',
            mode: 'key-value',
            visible: true,
          },
          {
            name: 'image_lora_fuse_kwargs',
            label: t('launchModel.loraFuseKwargsForImageModel'),
            type: 'dynamicField',
            mode: 'key-value',
            visible: true,
          },
        ],
      },
      {
        name: 'nested_section_virtualEnv',
        label: t('launchModel.virtualEnvConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'enable_virtual_env',
            label: t('launchModel.modelVirtualEnv'),
            type: 'radio',
            options: [
              { label: 'Unset', value: 'unset' },
              { label: 'False', value: false },
              { label: 'True', value: true },
            ],
            default: 'unset',
            visible: true,
          },
          {
            name: 'virtual_env_packages',
            label: t('launchModel.virtualEnvPackage'),
            type: 'dynamicField',
            mode: 'value',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
    ],
    audio: [
      {
        name: 'model_uid',
        label: t('launchModel.modelUID.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'replica',
        label: t('launchModel.replica'),
        type: 'number',
        visible: true,
        default: 1,
        inputProps: {
          inputProps: {
            min: 1,
          },
        },
      },
      {
        name: 'n_gpu',
        label: t('launchModel.device'),
        type: 'select',
        default: gpuAvailable === 0 ? 'CPU' : 'GPU',
        options: getNGPURange('audio'),
        visible: true,
      },
      {
        name: 'gpu_idx',
        label: t('launchModel.GPUIdx'),
        type: 'input',
        error: !!formData.gpu_idx && !/^\d+(?:,\d+)*$/.test(formData.gpu_idx),
        helperText: t('launchModel.enterCommaSeparatedNumbers'),
        visible: !!formData.n_gpu && formData.n_gpu === 'GPU',
      },
      {
        name: 'worker_ip',
        label: t('launchModel.workerIp.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'download_hub',
        label: t('launchModel.downloadHub.optional'),
        type: 'select',
        options: downloadHubOptions,
        visible: true,
      },
      {
        name: 'model_path',
        label: t('launchModel.modelPath.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'nested_section_virtualEnv',
        label: t('launchModel.virtualEnvConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'enable_virtual_env',
            label: t('launchModel.modelVirtualEnv'),
            type: 'radio',
            options: [
              { label: 'Unset', value: 'unset' },
              { label: 'False', value: false },
              { label: 'True', value: true },
            ],
            default: 'unset',
            visible: true,
          },
          {
            name: 'virtual_env_packages',
            label: t('launchModel.virtualEnvPackage'),
            type: 'dynamicField',
            mode: 'value',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'nested_section_env',
        label: t('launchModel.envVariableConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'envs',
            label: t('launchModel.envVariable'),
            type: 'dynamicField',
            mode: 'key-value',
            keyPlaceholder: 'key',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'custom',
        label: t('launchModel.additionalParametersForInferenceEngine'),
        type: 'dynamicField',
        mode: 'key-value',
        keyPlaceholder: 'key',
        valuePlaceholder: 'value',
        visible: true,
      },
    ],
    video: [
      {
        name: 'model_uid',
        label: t('launchModel.modelUID.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'replica',
        label: t('launchModel.replica'),
        type: 'number',
        visible: true,
        default: 1,
        inputProps: {
          inputProps: {
            min: 1,
          },
        },
      },
      {
        name: 'n_gpu',
        label: t('launchModel.device'),
        type: 'select',
        default: gpuAvailable === 0 ? 'CPU' : 'GPU',
        options: getNGPURange('video'),
        visible: true,
      },
      {
        name: 'gpu_idx',
        label: t('launchModel.GPUIdx'),
        type: 'input',
        error: !!formData.gpu_idx && !/^\d+(?:,\d+)*$/.test(formData.gpu_idx),
        helperText: t('launchModel.enterCommaSeparatedNumbers'),
        visible: !!formData.n_gpu && formData.n_gpu === 'GPU',
      },
      {
        name: 'worker_ip',
        label: t('launchModel.workerIp.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'download_hub',
        label: t('launchModel.downloadHub.optional'),
        type: 'select',
        options: downloadHubOptions,
        visible: true,
      },
      {
        name: 'model_path',
        label: t('launchModel.modelPath.optional'),
        type: 'input',
        visible: true,
      },
      {
        name: 'cpu_offload',
        label: t('launchModel.CPUOffload'),
        type: 'switch',
        default: false,
        tip: t('launchModel.CPUOffload.tip'),
        visible: true,
      },
      {
        name: 'nested_section_lora',
        label: t('launchModel.loraConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'lora_list',
            label: t('launchModel.loraModelConfig'),
            type: 'dynamicField',
            mode: 'key-value',
            keyPlaceholder: 'lora_name',
            valuePlaceholder: 'local_path',
            visible: true,
          },
          {
            name: 'image_lora_load_kwargs',
            label: t('launchModel.loraLoadKwargsForImageModel'),
            type: 'dynamicField',
            mode: 'key-value',
            visible: true,
          },
          {
            name: 'image_lora_fuse_kwargs',
            label: t('launchModel.loraFuseKwargsForImageModel'),
            type: 'dynamicField',
            mode: 'key-value',
            visible: true,
          },
        ],
      },
      {
        name: 'nested_section_virtualEnv',
        label: t('launchModel.virtualEnvConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'enable_virtual_env',
            label: t('launchModel.modelVirtualEnv'),
            type: 'radio',
            options: [
              { label: 'Unset', value: 'unset' },
              { label: 'False', value: false },
              { label: 'True', value: true },
            ],
            default: 'unset',
            visible: true,
          },
          {
            name: 'virtual_env_packages',
            label: t('launchModel.virtualEnvPackage'),
            type: 'dynamicField',
            mode: 'value',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'nested_section_env',
        label: t('launchModel.envVariableConfig'),
        type: 'collapse',
        visible: true,
        children: [
          {
            name: 'envs',
            label: t('launchModel.envVariable'),
            type: 'dynamicField',
            mode: 'key-value',
            keyPlaceholder: 'key',
            valuePlaceholder: 'value',
            visible: true,
          },
        ],
      },
      {
        name: 'custom',
        label: t('launchModel.additionalParametersForInferenceEngine'),
        type: 'dynamicField',
        mode: 'key-value',
        keyPlaceholder: 'key',
        valuePlaceholder: 'value',
        visible: true,
      },
    ],
  }

  return config
}
